using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERPWinFormWork.Test.Test.Dtos;
using ERPWinFormWork.Test.TestCode;

namespace ERPWinFormWork.Test
{
    /// <summary>
    /// 缺陷选择窗体：勾选缺陷后返回三类缺陷数量
    /// </summary>
    public class DefectSelectForm : Form
    {
        private readonly ApiClient _apiClient;
        private readonly Action<int, int, int> _onSelected; // (fatal, serious, minor)
        private DataGridView _grid;
        private Button _btnOk;
        private Button _btnCancel;

        public DefectSelectForm(Action<int, int, int> onSelected)
        {
            _apiClient = new ApiClient(TestAPI.TestApiBaseUrl);
            _onSelected = onSelected;
            InitializeUi();
            this.Load += async (s, e) => { await LoadDefectsAsync(); };
        }

        private void InitializeUi()
        {
            this.Text = "缺陷填报";
            this.Size = new Size(900, 600);
            this.StartPosition = FormStartPosition.CenterParent;

            _grid = new DataGridView
            {
                Dock = DockStyle.Top,
                Height = 480,
                AllowUserToAddRows = false,
                AutoGenerateColumns = false,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect,
                MultiSelect = true
            };

            var colCheck = new DataGridViewCheckBoxColumn { Name = "colCheck", HeaderText = "选择", Width = 60 };
            _grid.Columns.Add(colCheck);
            _grid.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Common_Defects_Name", HeaderText = "缺陷名称", Name = "colName", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
            _grid.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Type_Name", HeaderText = "缺陷类型", Name = "colType" });
            _grid.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Location_Name", HeaderText = "缺陷位置", Name = "colLoc"});
            _grid.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Level_Name", HeaderText = "缺陷等级", Name = "colLevel"});

            _btnOk = new Button { Text = "确定", Width = 80, Height = 32, Left = 700, Top = 500 };
            _btnOk.Click += (s, e) => Confirm();
            _btnCancel = new Button { Text = "取消", Width = 80, Height = 32, Left = 600, Top = 500 };
            _btnCancel.Click += (s, e) => this.Close();

            this.Controls.Add(_grid);
            this.Controls.Add(_btnOk);
            this.Controls.Add(_btnCancel);
        }

        private async Task LoadDefectsAsync()
        {
            var parameters = new Dictionary<string, string>
            {
                { "pagesize", "15" },
                { "pageindex", "1" }
            };

            var res = await _apiClient.GetAsync<TestApiResult<List<Common_DefectsDtos>>>(
                endpoint: "api/Read/GetCommon_Defects",
                parameters: parameters);

            var list = (res != null && res.code == 200 && res.pageDatas != null)
                ? res.pageDatas
                : new List<Common_DefectsDtos>();

            _grid.DataSource = new BindingList<Common_DefectsDtos>(list);
        }

        private void Confirm()
        {
            int fatal = 0, serious = 0, minor = 0;
            foreach (DataGridViewRow row in _grid.Rows)
            {
                var cell = row.Cells["colCheck"] as DataGridViewCheckBoxCell;
                var isChecked = cell != null && cell.Value != null && Convert.ToBoolean(cell.Value);
                if (!isChecked) continue;

                var level = Convert.ToString(row.Cells["colLevel"].Value ?? string.Empty);
                if (level.Contains("致命")) fatal++;
                else if (level.Contains("严重")) serious++;
                else if (level.Contains("轻微")) minor++;
            }

            _onSelected?.Invoke(fatal, serious, minor);
            this.Close();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // DefectSelectForm
            // 
            this.ClientSize = new System.Drawing.Size(1228, 766);
            this.Name = "DefectSelectForm";
            this.Text = "常见缺陷";
            this.ResumeLayout(false);

        }
    }
}


